using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 28/03/2010 10:46:38
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace Serix
{
   public class CLogOperacoes
   {
       private string DAT_HORA ;
       private string SEQ_LOG ;
       private string OPERACAO ;
       private string TABELA ;
       private string APLICACAO ;
       private string USUARIO ;
       private string CHAVE ;
       private string LOG ;

	   /// <sumary>
       /// Construtor da classe log_operacoes
       /// <sumary>
       public CLogOperacoes()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Log_operacoes
       /// <sumary>
       public CLogOperacoes( string DAT_HORA, string SEQ_LOG, string OPERACAO, string TABELA, string APLICACAO, string USUARIO, string CHAVE, string LOG )
       {
          this.DAT_HORA = DAT_HORA;
          this.SEQ_LOG = SEQ_LOG;
          this.OPERACAO = OPERACAO;
          this.TABELA = TABELA;
          this.APLICACAO = APLICACAO;
          this.USUARIO = USUARIO;
          this.CHAVE = CHAVE;
          this.LOG = LOG;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// 
        /// <sumary>
        public string Dat_hora 
		{
		   get
		   {
		       return this.DAT_HORA;
		   }
		   set
		   {
		       this.DAT_HORA = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Seq_log 
		{
		   get
		   {
		       return this.SEQ_LOG;
		   }
		   set
		   {
		       this.SEQ_LOG = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Operacao 
		{
		   get
		   {
		       return this.OPERACAO;
		   }
		   set
		   {
		       this.OPERACAO = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Tabela 
		{
		   get
		   {
		       return this.TABELA;
		   }
		   set
		   {
		       this.TABELA = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Aplicacao 
		{
		   get
		   {
		       return this.APLICACAO;
		   }
		   set
		   {
		       this.APLICACAO = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Usuario 
		{
		   get
		   {
		       return this.USUARIO;
		   }
		   set
		   {
		       this.USUARIO = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Chave 
		{
		   get
		   {
		       return this.CHAVE;
		   }
		   set
		   {
		       this.CHAVE = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Log 
		{
		   get
		   {
		       return this.LOG;
		   }
		   set
		   {
		       this.LOG = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Log_operacoes.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SEG_LOG_OPERACOES
			                  ( DAT_HORA, SEQ_LOG, OPERACAO, TABELA, APLICACAO, USUARIO, CHAVE, LOG )
						   values
						      ( :DAT_HORA, :SEQ_LOG, :OPERACAO, :TABELA, :APLICACAO, :USUARIO, :CHAVE, :LOG )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("DAT_HORA",OracleType.DateTime)); 
				command.Parameters.Add(new OracleParameter("SEQ_LOG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("OPERACAO",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("TABELA",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("APLICACAO",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("USUARIO",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("CHAVE",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("LOG",OracleType.Clob)); 
				
                command.Parameters["DAT_HORA"].Value = this.DAT_HORA;
                command.Parameters["SEQ_LOG"].Value = this.SEQ_LOG;
                command.Parameters["OPERACAO"].Value = this.OPERACAO;
                command.Parameters["TABELA"].Value = this.TABELA;
                command.Parameters["APLICACAO"].Value = this.APLICACAO;
                command.Parameters["USUARIO"].Value = this.USUARIO;
                command.Parameters["CHAVE"].Value = this.CHAVE;
                command.Parameters["LOG"].Value = this.LOG;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Log_operacoes.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SEG_LOG_OPERACOES set 
			                  ( OPERACAO = :OPERACAO, TABELA = :TABELA, APLICACAO = :APLICACAO, USUARIO = :USUARIO, CHAVE = :CHAVE, LOG = :LOG )
						   where
						      ( DAT_HORA = :DAT_HORA, SEQ_LOG = :SEQ_LOG )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("DAT_HORA",OracleType.DateTime)); 
				command.Parameters.Add(new OracleParameter("SEQ_LOG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("OPERACAO",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("TABELA",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("APLICACAO",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("USUARIO",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("CHAVE",OracleType.VarChar)); 
				command.Parameters.Add(new OracleParameter("LOG",OracleType.Clob)); 
				
                command.Parameters["DAT_HORA"].Value = this.DAT_HORA;
                command.Parameters["SEQ_LOG"].Value = this.SEQ_LOG;
                command.Parameters["OPERACAO"].Value = this.OPERACAO;
                command.Parameters["TABELA"].Value = this.TABELA;
                command.Parameters["APLICACAO"].Value = this.APLICACAO;
                command.Parameters["USUARIO"].Value = this.USUARIO;
                command.Parameters["CHAVE"].Value = this.CHAVE;
                command.Parameters["LOG"].Value = this.LOG;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Log_operacoes.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SEG_LOG_OPERACOES", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("DAT_HORA", DbType.Int32, this.DAT_HORA));
            tab.adicionarFiltro(new Coluna("SEQ_LOG", DbType.Int32, this.SEQ_LOG));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SEG_LOG_OPERACOES order by  ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CLogOperacoes PesquisaporCodigo(string DAT_HORA, string SEQ_LOG, string login)
        {
            CLogOperacoes obj;
            string SQL = @"SELECT *
                            FROM  Serix.SEG_LOG_OPERACOES 
                            WHERE DAT_HORA = :DAT_HORA, SEQ_LOG = :SEQ_LOG";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":DAT_HORA", DbType.String, DAT_HORA);
				qy.AdicionarParametro(":SEQ_LOG", DbType.String, SEQ_LOG);
               
				qy.Executar();
                obj = new CLogOperacoes( qy.ObterCampo("DAT_HORA"), qy.ObterCampo("SEQ_LOG"), qy.ObterCampo("OPERACAO"), qy.ObterCampo("TABELA"), qy.ObterCampo("APLICACAO"), qy.ObterCampo("USUARIO"), qy.ObterCampo("CHAVE"), qy.ObterCampo("LOG") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CLogOperacoes();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt()
        {
		    string login = "serix";
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_TER() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
    }
}	

